#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int pipefd[2] = {0};
    int n = pipe(pipefd);
    if(n == -1)
    {
        perror("pipe error\n");
        exit(EOF);
    }
    int pid = fork();
    if(pid == 0) //子进程读取
    {
        close(pipefd[1]); //关闭写端
        char s[1024] = {0};
        read(pipefd[0],s,sizeof(s)-1);
        printf("%s\n",s);
        close(pipefd[0]); //最后关闭读端
        exit(1);
    }
    //父进程写入
    close(pipefd[0]); //关闭读端
    const char* str = "i am father";
    write(pipefd[1],str,sizeof(char)*strlen(str));
    close(pipefd[1]); //最后关闭写端
    return 0;
}